package com.kaichaohulian.baocms.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.kaichaohulian.baocms.R;
import com.kaichaohulian.baocms.entity.ShowCityBean;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * Created by Angel on 2017/9/12.
 */

public class ChooseCityAdapter extends RecyclerView.Adapter<ChooseCityAdapter.ViewHolder> implements View.OnClickListener{


    private Context context;
    private List<ShowCityBean> list = new ArrayList<>();

    public ChooseCityAdapter(Context context) {
        this.context = context;
    }
    private OnItemClickListener mOnItemClickListener = null;
    public void refreshDatas(List<ShowCityBean> list) {
        this.list.clear();
        this.list.addAll(list);
        notifyDataSetChanged();

    }

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
//        return new ViewHolder(((LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE)).inflate(R.layout.item_title, parent, false));
        View view = LayoutInflater.from(context).inflate(R.layout.item_title, parent, false);
        ViewHolder vh = new ViewHolder(view);
        //将创建的View注册点击事件
        view.setOnClickListener(this);
        return vh;

    }
    public void setOnItemClickListener(OnItemClickListener listener) {
        this.mOnItemClickListener = listener;
    }
    @Override
    public void onBindViewHolder(ViewHolder holder, int position) {
        holder.loadDatas(list.get(position));
        holder.itemView.setTag(position);
    }

    @Override
    public int getItemCount() {
        return list.size();
    }

    @Override
    public int getItemViewType(int position) {
        return list.get(position).getType();
    }

    @Override
    public void onClick(View v) {
        if (mOnItemClickListener != null) {
            //注意这里使用getTag方法获取position
            mOnItemClickListener.onItemClick(v,(int)v.getTag());
        }
    }

    class ViewHolder extends RecyclerView.ViewHolder {

        @BindView(R.id.tv_title)
        TextView tvTitle;

        public ViewHolder(View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
        }

        public void loadDatas(ShowCityBean showCityBean) {
            tvTitle.setText(showCityBean.getCnName());
            tvTitle.setBackgroundColor(showCityBean.getType() == ShowCityBean.TYPE_CITY ? 0xffffffff : 0xfff2f2f2);
            ((ViewGroup) tvTitle.getParent()).setBackgroundColor(showCityBean.getType() == ShowCityBean.TYPE_CITY ? 0xffffffff : 0xfff2f2f2);
        }
    }


    //define interface
    public static interface OnItemClickListener {
        void onItemClick(View view , int position);
    }


}
